import { getEnginePlugin, getInitGisAppConfig, initParam3d } from './param3d';
import { PluginObject } from '../type';
import { getCurrentInstance, onMounted, ref, Ref } from 'vue';
const useGis = (pluginName:string) => {
  const PluginYLYLH = getEnginePlugin()[pluginName];

  const gisAppRef: Ref<HTMLDivElement | undefined> = ref(undefined);
  const internalInstance = getCurrentInstance();
  let gisApp: any;
  /** 地图是否初始完成 */
  const isInit = ref(false);
  const customPlugin: { value?: PluginObject } = { value: undefined };
  /**初始化gisApp */
  const initGisApp = () => {
    initParam3d().then(async () => {
      if (internalInstance && internalInstance.isUnmounted) return;
      const initGisAppConfig = getInitGisAppConfig(gisAppRef.value);
      gisApp = new (window as any).Param3DEngine.GISApp(initGisAppConfig);
      customPlugin.value = new PluginYLYLH();
      gisApp.pluginServer.addPlugin(customPlugin.value);
      isInit.value = true;
    });
  };
  onMounted(() => {
    initGisApp();
  });
  return {
    gisAppRef,
    customPlugin,
    isInit,
  };
};
export default useGis;
